 /*****************************************************************
*	Copyright (C) Jerry. All rights reserved.
*	
*	
*	Project Name:
*
*	File Name: com_port.h
*
*	Module Name:
*
*	Description:
*		Communication port header file
*
*	History:
*	Name		Date			Version			Note	
*	-------		---------	--------		-----------
*	Jerry		2004-04-29	Initial version	
*	
****************************************************************/
#include "stdafx.h"

#ifndef	__COM_PORT_H_
#define	__COM_PORT_H_

#ifdef __cplusplus
extern "C" {
#endif

#define COM_PORT_MAX_NUM			16		// Total com_port 
#define MAX_INPUT_BUF_SIZE			2048
#define MAX_OUTPUT_BUF_SIZE			2048

/**************************************************************/
// --- PC and PS corresponding baud-rate ----
typedef struct _tag_baud_rate_param {
	DWORD	m_pc_baud;
	BYTE	m_ps_baud;
} T_BAUD_RATE;

// the parameters of serial port.
typedef struct Serial_Port_Param
{
	DWORD	dwBaud;
	BYTE	byParity;
	BYTE	byDataBits;
	BYTE	byStopBits;
} SERIAL_PORT_PARAM, *P_SERIAL_PORT_PARAM;


class CCommPort
{

public:
	HANDLE	m_hPortHandle;
	BYTE	byPortName[10];	

protected:
	SERIAL_PORT_PARAM	m_PortParam;

	COMMTIMEOUTS	stCommTimeOut;

public:
	CCommPort();
	CCommPort(BYTE *pPortName);
	CCommPort(BYTE *pPortName, P_SERIAL_PORT_PARAM pstPortParam);
	~CCommPort();

public:
	VOID SetParam(const SERIAL_PORT_PARAM &stParam);/* set the serial port parameters */
	BOOL OpenPort();					/* open the serial port */
	BOOL ClosePort();					/* close the serial port */
	int		GetBufferDataCount();
};
/**************************************************************/
extern INT m_baud_rate_index;
extern const T_BAUD_RATE g_baud_rate_table[];

extern INT WriteDataToCom(HANDLE	hPortHandle, BYTE *pData, INT length);
extern INT ReadDataFromCom( HANDLE hPortHandle, BYTE *pData, INT length);
extern BOOL ClearRxBuffer( HANDLE hPortHandle );
extern BOOL ClearTxBuffer( HANDLE hPortHandle );

#ifdef __cplusplus
}
#endif

#endif		// --- end __COM_PORT_H_

